package com.zx.idc.backend.gui.api.watch.handler;

import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.management.basicdata.BaseIdcMonitorDataService;
import com.zx.idc.backend.gui.api.watch.handler.base.AbstractFtpUploadResultHandler;
import com.zx.idc.backend.gui.api.watch.pojo.FtpUploadResult;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.xml.entity.idcmonitor.IdcMonitor;
import com.zx.idc.xml.utils.JaxbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.zx.idc.common.attributes.FTPReportDataTypeAttributes.FTP_BASE_DATA_MONITORING_ABNORMAL_RECORDS;

/**
 * 基础数据监测异常记录结果处理器
 */
@Component
//@Transactional(rollbackFor = Exception.class) //不需要声明事务,当前的类相当于controller
public class BaseDataMonitorAbnormalRecordsResultHandler extends AbstractFtpUploadResultHandler {
    @Autowired
    private BaseIdcMonitorDataService baseIdcMonitorDataService;

    /**
     * 匹配
     *
     * @param type
     * @return
     */
    @Override
    public boolean adapter(String type) {
        return FTP_BASE_DATA_MONITORING_ABNORMAL_RECORDS.equals(type);
    }

    /**
     * 上报成功回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadSuccess(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("basic data monitoring abnormal record result success processor start!");
        final IdcMonitor idcMonitor = JaxbUtils.xmlFileToBean(filePath, IdcMonitor.class);
        baseIdcMonitorDataService.update(idcMonitor, ReportStatusConstant.REPORTED);
        log.debug("basic data monitoring abnormal record result success processor done!");
    }

    /**
     * 上报异常回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadFailed(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("basic data monitoring abnormal record result failed processor start!");
        final IdcMonitor idcMonitor = JaxbUtils.xmlFileToBean(filePath, IdcMonitor.class);
        baseIdcMonitorDataService.update(idcMonitor, ReportStatusConstant.REPORTING_EXCEPTION);
        log.debug("basic data monitoring abnormal record result failed processor done!");
    }
}
